package com.akk.test;
import com.akk.bean.Department;
import com.akk.bean.Employee;
import com.akk.dao.DepartmentMapper;
import com.akk.dao.EmployeeMapper;

import java.util.UUID;

import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


/*
  *  測試dao層的工作
  *  推薦spring 的項目就可以使用	Spring的單元測試，可以自動注入我們需要的組件
 * 1.導入SpringTest模塊
 * 2.@ContextConfiguration指定spring配置文件的位置
 * 
 * 
 * */

// @ContextConfiguration(locations指定spring配置文件位置，这样不用自己获得spring的ioc容器
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
	public class MMapper {
	    @Autowired
	    DepartmentMapper departmentMapper;
	    @Autowired
	    EmployeeMapper employeeMapper;
	    @Autowired
	    SqlSession sqlSession;
	    // 测试DepartmentMapper
	    @Test
	    public void testCRUD() throws Exception {
	    	
//	    	System.out.println(departmentMapper);
	    	/**舊的方法
	    	//1.創建springIOC容器
	    	ApplicationContext ioc= new ClassPathXmlApplicationContext("applicationContext.xml");
	    	//2.從容器中獲取mapper
	    	DepartmentMapper bean = ioc.getBean(DepartmentMapper.class);
	    	**/
//	    	departmentMapper.insertSelective(new Department(null,"董事長"));
//	    	departmentMapper.insertSelective(new Department(null,"部門經理"));
//	    	departmentMapper.insertSelective(new Department(null,"研發部"));
//	    	departmentMapper.insertSelective(new Department(null,"測試部"));
//	    	departmentMapper.insertSelective(new Department(null,"銷售部"));
//	    	departmentMapper.insertSelective(new Department(null,"人資部"));
//	    	departmentMapper.insertSelective(new Department(null,"福委會"));
//	    	departmentMapper.insertSelective(new Department(null,"廢物部"));
	    	
//	    		        System.out.println(departmentMapper);
	        // 如果插入中文乱码，在dbconfig.properties文件中的url中配置?characterEncoding=utf8;
//	        System.out.println("end");
	    	
	        EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
	        for (int i = 0; i < 1000; i++) {
	            String uid = UUID.randomUUID().toString().substring(0,5) + i;
	            employeeMapper.insertSelective(new Employee(null, uid, "M", uid + "@qq.com", 1));
	        }
	        System.out.println("end");

	    }
}